<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>在查询中使用已索引的形状 | Elasticsearch: 权威指南 | Elastic</title>
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
</head>
<body>
<div class="main-container">
    <section id="content">
        
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原文地址: <a href="https://www.elastic.co/guide/cn/elasticsearch/guide/current/indexed-geo-shapes.html" rel="nofollow">https://www.elastic.co/guide/cn/elasticsearch/guide/current/indexed-geo-shapes.html</a>, 版权归 www.elastic.co 所有<br/>
                            英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/guide/current/indexed-geo-shapes.html" rel="nofollow">https://www.elastic.co/guide/en/elasticsearch/guide/current/indexed-geo-shapes.html</a>
                            </div>
                        <!-- start body -->
                  <div class="page_header">
<b>请注意:</b><br>本书基于 Elasticsearch 2.x 版本，有些内容可能已经过时。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch: 权威指南</a></span>
»
<span class="breadcrumb-link"><a href="geoloc.html">地理位置</a></span>
»
<span class="breadcrumb-link"><a href="geo-shapes.html">地理形状</a></span>
»
<span class="breadcrumb-node">在查询中使用已索引的形状</span>
</div>
<div class="navheader">
<span class="prev">
<a href="querying-geo-shapes.html">« 查询地理形状</a>
</span>
<span class="next">
<a href="modeling-your-data.html">数据建模 »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="indexed-geo-shapes"></a>在查询中使用已索引的形状<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/340_Geoshapes/78_Indexed_geo_shapes.asciidoc">edit</a>
</h2>
</div></div></div>
<p>对于那些经常会在查询中使用的形状，可以把它们索引起来以便在查询中可以方便地直接引用名字。以之前的阿姆斯特丹中部为例，我们可以把它存储成一个类型为 <code class="word">neighborhood</code> 的文档。</p>
<p>首先，我们仿照之前设置 <code class="word">landmark</code> 时的方式建立映射：</p>
<div class="pre_wrapper lang-json">
<pre class="programlisting prettyprint lang-json">PUT /attractions/_mapping/neighborhood
{
  "properties": {
    "name": {
      "type": "string"
    },
    "location": {
      "type": "geo_shape"
    }
  }
}</pre>
</div>
<p>然后我们索引阿姆斯特丹中部对应的形状：</p>
<div class="pre_wrapper lang-json">
<pre class="programlisting prettyprint lang-json">PUT /attractions/neighborhood/central_amsterdam
{
  "name" : "Central Amsterdam",
  "location" : {
      "type" : "polygon",
      "coordinates" : [[
        [4.88330,52.38617],
        [4.87463,52.37254],
        [4.87875,52.36369],
        [4.88939,52.35850],
        [4.89840,52.35755],
        [4.91909,52.36217],
        [4.92656,52.36594],
        [4.93368,52.36615],
        [4.93342,52.37275],
        [4.92690,52.37632],
        [4.88330,52.38617]
      ]]
  }
}</pre>
</div>
<p>形状索引好之后，我们就可以在查询中通过 <code class="literal">index</code> ， <code class="literal">type</code> 和 <code class="literal">id</code> 来引用它了：</p>
<div class="pre_wrapper lang-json">
<pre class="programlisting prettyprint lang-json">GET /attractions/landmark/_search
{
  "query": {
    "geo_shape": {
      "location": {
        "relation": "within",
        "indexed_shape": { <a id="CO247-1"></a><i class="conum" data-value="1"></i>
          "index": "attractions",
          "type":  "neighborhood",
          "id":    "central_amsterdam",
          "path":  "location"
        }
      }
    }
  }
}</pre>
</div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO247-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>指定 <code class="literal">indexed_shape</code> 而不是 <code class="literal">shape</code> ，Elasticesearch 就知道需要从指定的文档和 <code class="literal">path</code> 检索出对应的形状了。</p>
</td>
</tr>
</table>
</div>
<p>阿姆斯特丹中部这个形状没有什么特别的。同样地，我们也可以在查询中使用已经索引好的达姆广场。这个查询可以找出与达姆广场有交集的临近点：</p>
<div class="pre_wrapper lang-json">
<pre class="programlisting prettyprint lang-json">GET /attractions/neighborhood/_search
{
  "query": {
    "geo_shape": {
      "location": {
        "indexed_shape": {
          "index": "attractions",
          "type":  "landmark",
          "id":    "dam_square",
          "path":  "location"
        }
      }
    }
  }
}</pre>
</div>
</div>
<div class="navfooter">
<span class="prev">
<a href="querying-geo-shapes.html">« 查询地理形状</a>
</span>
<span class="next">
<a href="modeling-your-data.html">数据建模 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>